/***************************************************************************
 *   Copyright (C) 2006 2007 by ywchen2000                         *
 *   ywchen2000@yahoo.com.cn                                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include<QtGui>
#include"RclXml.h"
const char* childname="project";
const char* project_dir="projdir";
rclxml::rclxml(QWidget* parent):QObject(parent),dir(QString())
{
	this->parent=parent;
}
rclxml::~rclxml(void)
{
}
bool rclxml::read(QIODevice *device,QList<QString>& l)
{
	QString errorStr;
	int errorLine;
	int errorColumn;
	if (!domDocument.setContent(device, true, &errorStr, &errorLine,&errorColumn)) 
	{
        	QMessageBox::information(parent,tr("RCL PROJECT"),
                                 tr("Parse error at line %1, column %2:\n%3")
                                 .arg(errorLine)
                                 .arg(errorColumn)
                                 .arg(errorStr));
        	return false;
    	}
        QDomElement root=domDocument.documentElement();
	if(root.tagName()!="rclproj")
	{
		QMessageBox::information(parent,tr("RCL PROJECT"),tr("The file is not an rclproj file."));
		return false;
	}	
	else if (root.hasAttribute("version") && root.attribute("version") != "1.0") 
	{
        	QMessageBox::information(parent, tr("RCL PROJECT"),tr("The file is not an rclproj version 1.0 file."));
        	return false;
	}
	QDomElement child=root.firstChildElement(childname);  // get project
	child=child.firstChildElement(project_dir);           //get project dir
	dir=child.text();
	//now get files of project
        child=child.nextSiblingElement();
	get_file(child,l);
	return true;
}
bool rclxml::read(QIODevice *device,tree& l)
{
	QString errorStr;
	int errorLine;
	int errorColumn;
	if (!domDocument.setContent(device, true, &errorStr, &errorLine,&errorColumn)) 
	{
        	QMessageBox::information(parent,tr("RCL PROJECT"),
                                 tr("Parse error at line %1, column %2:\n%3")
                                 .arg(errorLine)
                                 .arg(errorColumn)
                                 .arg(errorStr));
        	return false;
    	}
        QDomElement root=domDocument.documentElement();
	if(root.tagName()!="rclproj")
	{
		QMessageBox::information(parent,tr("RCL PROJECT"),tr("The file is not an rclproj file."));
		return false;
	}	
	else if (root.hasAttribute("version") && root.attribute("version") != "1.0") 
	{
        	QMessageBox::information(parent, tr("RCL PROJECT"),tr("The file is not an rclproj version 1.0 file."));
        	return false;
	}
	QDomElement child=root.firstChildElement(childname);  // get project
	child=child.firstChildElement(project_dir);           //get project dir
	dir=child.text();
	//now get files of project
        child=child.nextSiblingElement();
	get_file(child,l);
	return true;
}
bool rclxml::write(QIODevice *device,const QString &rootdir,const QList<QString> &list)
{
	QTextStream out;
        device->open(QIODevice::ReadWrite | QIODevice::Text);
	out.setDevice(device);
	out.setCodec("UTF-8");
	out<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
	   <<"<!DOCTYPE	rclproj>\n"
	   <<"<rclproj version=\"1.0\">\n"
	   <<indent(1)<<"<project>\n"
 	   <<indent(2)<<"<projdir>"<<rootdir<<"</projdir>\n"; 	            			
	for(register int i=0;i<list.size();i++)  	
	  	{
			out<<indent(3)<<"<file>"<<list.at(i)<<"</file>\n";
		}
	out<<indent(1)<<"</project>\n";
	out<<"</rclproj>\n";
        out.flush();
	return true;
}
QString rclxml::indent(int depth)
{
	static const int IndentSize = 4;
    	return QString(IndentSize * depth, ' ');
}
QString rclxml::get_dir(void)const
{
	return dir;
}
void rclxml::get_file(const QDomElement& element,tree& l)
{
	QDomElement child=element;
	while(!child.isNull())
	{
		l.insert(child.text());
		child=child.nextSiblingElement();
	}
}
void rclxml::get_file(const QDomElement& element,QList<QString>& l)
{
    QDomElement child=element;	
    while(!child.isNull())
	{
         	l.push_front(child.text());
		child=child.nextSiblingElement();
	}	
}
